package mua.core.operation.basis;

import mua.core.value.Bool;
import mua.core.value.MuaValue;
import mua.core.value.Number;
import mua.core.value.Word;

public class Operator {
    public static MuaValue add(MuaValue a, MuaValue b){
        return new Number(String.valueOf((double)(a.getValue())+(double)(b.getValue())));
    }

    public static MuaValue sub(MuaValue a, MuaValue b){
        return new Number(String.valueOf((double)(a.getValue())-(double)(b.getValue())));
    }

    public static MuaValue mul(MuaValue a, MuaValue b){
        return new Number(String.valueOf((double)a.getValue()*(double)b.getValue()));
    }

    public static MuaValue div(MuaValue a, MuaValue b){
        return new Number(String.valueOf((double)a.getValue()/(double)b.getValue()));
    }

    public static MuaValue mod(MuaValue a, MuaValue b){
        return new Number(String.valueOf((double)a.getValue()%(double)b.getValue()));
    }

    //eq, gt, lt 能够按照字典序比较字符串a和b
    public static MuaValue eq(MuaValue a, MuaValue b) {
        if(a instanceof Word){
            return new Bool(String.valueOf(a.getValue().equals(b.getValue())));
        }else if(a instanceof Number){
            return new Bool(String.valueOf((double)a.getValue()==(double)b.getValue()));
        }
        return new Bool("false");
    }

    public static MuaValue gt(MuaValue a, MuaValue b) {
        if(a instanceof Word){
            return new Bool(String.valueOf(a.getValue().toString().compareTo(b.getValue().toString())>0));
        }else if(a instanceof Number){
            return new Bool(String.valueOf((double)a.getValue()>(double)b.getValue()));
        }
        return new Bool("false");
    }

    public static MuaValue lt(MuaValue a, MuaValue b) {
        if(a instanceof Word){
            return new Bool(String.valueOf(a.getValue().toString().compareTo(b.getValue().toString())<0));
        }else if(a instanceof Number){
            return new Bool(String.valueOf((double)a.getValue()<(double)b.getValue()));
        }
        return new Bool("false");
    }

    //and, or, not 后跟布尔类型
    public static MuaValue and(MuaValue a, MuaValue b) {
        if(a.toString().equals("false") || b.toString().equals("false")){
            return new Bool("false");
        }
        return new Bool("true");
    }

    public static MuaValue or(MuaValue a, MuaValue b) {
        if(a.toString().equals("true") || b.toString().equals("true")){
            return new Bool("true");
        }
        return new Bool("false");
    }

    public static MuaValue not(MuaValue a){
        if(a.toString().equals("true")){
            return new Bool("false");
        }else{
            return new Bool("true");
        }
    }
}
